<?php
namespace app\admin\model;
use app\facade\Power;
use think\Model;
class User extends Model
{

    //获取用户表数据
    public function get_user(){

        $res = $this->table('d_user')->select();
        return $res;
    }




    //验证与处理添加用户信息
    public function get_adds($posts){
        //判断必填项不可为空
        if($posts['pass_word']=='' || $posts['user_name']==''){
            return 999;
        }else{
            //给密码MD5加密
            $posts['pass_word'] = md5(md5($posts['pass_word']));
            $posts['create_time'] = time();
            //判断是否存在相同的用户名
            $judge['user_name'] = $posts['user_name'];
            $arr = $this->table('d_user')->where($judge)->select();
            if($arr->isEmpty()){
                //设置权限
                $power = new \app\admin\controller\common\Power($posts['auth']);
                if (Power::get_auth_level() < $power->get_auth_level()){
                    //设置权限名称
                    $power->auth_data['auth_name'] = $posts['user_name'];
                    //设置权限模板为0
                    $power->auth_data['auth_model'] = 0 ;
                    unset($power->auth_data['id']);
                    $posts['auth'] = $power->set_auth_model();
                }else{
                    echo '权限不足';
                }

                $res = $this->table('d_user')->insert($posts);
                return $res;
            }else{
                return 999;
            }
        }
    }


    //修改用户信息
    public function get_edits($posts,$ids){
        //判断必填项不可为空
        if($posts['pass_word']=='' || $posts['user_name']==''){
            return 999;
        }else{
            //给密码MD5加密
            $posts['pass_word'] = md5(md5($posts['pass_word']));
            $posts['create_time'] = time();
            $res = $this->table('d_user')->where('id='.$ids)->update($posts);
            return $res;
        }
    }

}